package com.pan.controller;


import com.pan.annotation.LoginRequired;
import com.pan.entity.OrderTable1;
import com.pan.entity.Result;
import com.pan.service.OrderCreateService;
import com.pan.service.OrderSelectService;
import com.pan.service.OrderUpdateService;
import com.pan.vo.CreateOrderInfoVo;
import com.pan.vo.OrderItemVo;
import com.pan.vo.OrderVo;
import com.pan.vo.UpdateStatusVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderSelectService orderSelectService;

    @Resource
    private OrderUpdateService orderUpdateService;

    @Resource
    private OrderCreateService orderCreateService;

    @ResponseBody
    @GetMapping("/select/{order1Id}")
    @LoginRequired
    public Result<OrderVo> selectById(@PathVariable Long order1Id){
        Result result = orderSelectService.selectAllInfoById(order1Id);
        return result;
    }


    @ResponseBody
    @GetMapping("/select/user")
    @LoginRequired
    public Result<List<OrderTable1>> selectByUserId(@CookieValue("userTicket") String userTicket){
        Result<List<OrderTable1>> result = orderSelectService.selectByUserId(userTicket);
        return result;
    }


    @PostMapping("/create/useCoupon")
    @ResponseBody
    @LoginRequired
    public Result createOrderUseCoupon(@CookieValue("userTicket") String userTicket,
                                    @RequestBody CreateOrderInfoVo createOrderInfoVo){
        List<OrderItemVo> orderItemVos = createOrderInfoVo.getOrderItemVos();
        Long couponHistoryId = createOrderInfoVo.getCouponHistoryId();
        String address = createOrderInfoVo.getAddress();
        Result result = orderCreateService.createOrder(userTicket,address,orderItemVos,couponHistoryId);
        return result;
    }

    @PostMapping("/create/{address}")
    @ResponseBody
    @LoginRequired
    public Result createOrder(@CookieValue("userTicket") String userTicket,
                                       @PathVariable String address,
                                       @RequestBody List<OrderItemVo> orderItemVos){

        Result result = orderCreateService.createOrderNoCoupon(userTicket,address,orderItemVos);
        return result;
    }

    @ResponseBody
    @LoginRequired
    @PostMapping("/update/order1/user/status")
    public Result updateOrder1Status(@CookieValue("userTicket") String userTicket,
                                     @RequestBody UpdateStatusVo updateStatusVo){
        Integer status = updateStatusVo.getStatus();
        Long order1Id = updateStatusVo.getId();
        Result result = orderUpdateService.updateOrder1Status(userTicket,order1Id,status);
        return result;
    }


    @ResponseBody
    @LoginRequired
    @PostMapping("/update/order2/user/status")
    public Result updateOrder2Status(@CookieValue("userTicket") String userTicket,
                                     @RequestBody UpdateStatusVo updateStatusVo) {
        Integer status = updateStatusVo.getStatus();
        Long order2Id = updateStatusVo.getId();
        Result result = orderUpdateService.updateOrder2Status(userTicket,order2Id,status);
        return result;
    }
}
